import StoreShop from "../module/StoreShopCard";
import { ActionType } from "../../index";
import { fromJS } from "immutable";
import _ from "lodash";

/**
 * 获取商城布局和数据接口。这里需要针对数据进行整合
 * @param componentId 商城唯一识别
 * @param data 入参数据
 * @returns
 */
export function init(componentId: string, data: StoreShop) {
  return async (dispatch: any) => {
    if (data) {
      dispatch({
        type: ActionType.STORE_SHOP_INIT,
        componentId,
        data: data.getData(),
        layout: data.getLayout(),
      });
    }
  };
}

export function cardCheck(componentId: string, data: any) {
  return async (dispatch: any, getState: any) => {
    let shopCard = fromJS(getState().reducer).getIn([
      "instances",
      componentId,
      "shopCard",
    ]);

    if (!shopCard) {
      shopCard = [];
    }
    shopCard.push(data);
    if (data) {
      dispatch({
        type: ActionType.STORE_SHOP_CARD_CHECK,
        componentId,
        shopCard: _.cloneDeep(shopCard),
      });
    }
  };
}
